package com.gugee.tiktok.data.consumer.music.core.spider;


import com.gugee.tiktok.common.model.spider.music.QrCodeResponse;
import com.xiguaji.boot.retry.RetryableException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

//@Component
public class TikCodeSpiderClient {
    @Autowired
    RestTemplate restTemplate;

    @Value("${xiguaji.spider.host}")
    String host;

    @Retryable(value = RetryableException.class, maxAttempts = 10)
    public QrCodeResponse get(long id, int type){
        String uri = host + "/Aweme/GetTikCode?schema_type={type}&object_id={id}";
        ResponseEntity<QrCodeResponse> responseEntity = restTemplate.getForEntity(uri, QrCodeResponse.class,type,id);

        if(responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new RetryableException(String.format("search tikCode error. status: %s . msg: %s", responseEntity.getStatusCodeValue(), responseEntity.getBody()));
        }

        if(responseEntity.getBody() == null ){
            throw new RetryableException("search tikCode error");
        }
        return responseEntity.getBody();
    }
}